package router

import (
	"github.com/gin-gonic/gin"
	"go-demo-2025/service/message_queue/kafka_demo1/check_message_back"
	"go-demo-2025/service/message_queue/kafka_demo1/local_message"
)

func KafkaDemoRouter(e *gin.Engine) {
	kafkaDemo1 := e.Group("/kafka_demo1")
	{
		kafkaDemo1.POST("/create-order", func(c *gin.Context) {
			var req struct {
				OrderID string `json:"order_id" binding:"required"`
				Amount  int64  `json:"amount" binding:"required,min=1"`
			}
			if err := c.ShouldBindJSON(&req); err != nil {
				c.JSON(400, gin.H{"error": err.Error()})
				return
			}

			err := local_message.CreateOrderAndRecordMessage(c.Request.Context(), req.OrderID, req.Amount)
			if err != nil {
				c.JSON(500, gin.H{"error": err.Error()})
				return
			}

			c.JSON(200, gin.H{"success": true, "order_id": req.OrderID})
		})
		// 回查机制相关接口
		kafkaDemo1.POST("/create-order2", check_message_back.CreateOrderHandler)
		kafkaDemo1.POST("/callback", check_message_back.CallbackHandler)
	}
}
